$(document).ready(function(){
	$('#side-menu').metisMenu();
	//Loads the correct sidebar on window load,
	//collapses the sidebar on window resize.
	$(window).bind("load resize", function() {
		width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width;
		if (width < 768) {
			$('div.sidebar-collapse').addClass('collapse')
		} else {
			$('div.sidebar-collapse').removeClass('collapse')
		}
	});
	
	//Get category type ajax 
	$('#CategoryCatType').change(function(){
		// selected value
		var selected = $(this).val();
		$.ajax({
			type: "POST",
			url: WEB_ROOT + 'Categorys/getCategoryType',
			data: "ajax=true&cat_type="+selected,
			success: function(msg){
				$('.category_type').html(msg);
			}
		});
	});
	
	//update field active and news hot
	$('.news_update_active, .news_update_hot').click(function(){
		$href = $(this).attr('href');
		var $that = $(this);
		var value = $that.children('img').attr('value');
		var title = $('.tipBody').html();
		$that.html('<img src="/admin/img/indicator.gif" border="0">');
		$.ajax({
			type: "POST",
			url: $href,
			data: "ajax=true&value="+value,
			success: function(msg){
				$that.attr('title', title);
				$that.html('<img src="/admin/img/check_'+ msg +'.gif" border="0" value="'+ Math.abs(msg - 1) +'">');
			}
		});
		
		return false;
	});
	
	//check all checkbox
	$('#check-all').click(function(){
		if ($(this).attr('checked') == true) {
			$('.item-element').attr('checked', true);
		} else {
			$('.item-element').attr('checked', false);
		}
	});
	
	//create link
	$('#create_link').click( function(){
		$href = $(this).attr('href');
		window.open($href, "Link","height=900,width=800,menubar=0,resizable=1,scrollbars=1,statusbar=0,titlebar=0,toolbar=0");
	});
	
	//Tooltip
	//Select all anchor tag with rel set to tooltip
	$('a[rel=tooltip]').mouseover(function(e) {
		
		//Grab the title attribute's value and assign it to a variable
		var tip = $(this).attr('title');	
		
		//Remove the title attribute's to avoid the native tooltip from the browser
		$(this).attr('title', '');
		
		//Append the tooltip template and its value
		$(this).append('<div id="tooltip"><div class="tipBody">' + tip + '</div></div>');

		//Show the tooltip with faceIn effect
		//$('#tooltip').fadeIn('500');
		//$('#tooltip').fadeTo('10',0.9);
		
	}).mousemove(function(e) {
	
		//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
		$('#tooltip').css('top', e.pageY + 25 );
		$('#tooltip').css('left', e.pageX - 30 );
		
	}).mouseout(function() {
		//Put back the title attribute's value
		$(this).attr('title', $('.tipBody').html());
	
		//Remove the appended tooltip template
		$(this).children('#tooltip').remove();
	});
	
	//create link menu admin
	$('#change_category').change(function(){
		// selected value
		var iCat = $(this).val();
		$("#link_category").html($.trim($('#category' + iCat).html()));
		$.ajax({
			type: "POST",
			url: WEB_ROOT + 'createLink/createLinkCategory',
			data: "ajax=true&iCat="+iCat,
			success: function(msg){
				$('.response-ajax-create-link').html(msg);
			}
		});
	});

});